import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import sun.rmi.runtime.Log;

public class MarioBros extends Sprite {
    GameScene gameScene;
    Fx fx;
    boolean leftYD, rightYD;
    public static boolean Jump, Jump1;
    boolean Dm, Rc;
    public static boolean Bj;
    int YD = 0;
    public static int SuDu = 5;
    double jx, jy;
    public static boolean pzjc;// 碰撞检测
    public boolean El;// 判断是否在砖头上面
    public boolean Elxl;
    private static Image[] imageleft = new Image[]{
            new Image("mari_left2.png"),
            new Image("mari_left3.png"),
            new Image("mari_left4.png")

    };
    private static Image[] imageright = new Image[]{
            new Image("mari2.png"),
            new Image("mari3.png"),
            new Image("mari4.png")

    };

    public MarioBros(double x, double y, Fx fx, GameScene gameScene) {
        super(null, x, y, 60, 60);
        this.gameScene = gameScene;
        this.fx = fx;

    }

    private int moveidnum = 0;

    @Override
    public void pain(GraphicsContext graphicsContext) {
        if (fx.equals(Fx.stop)) {
            YD = 0;
            image = new Image("mari1.png");
        } else if (fx.equals(Fx.left)) {
            image = imageleft[YD];
            moveidnum++;
            if (moveidnum > 5) {
                YD++;
                moveidnum = 0;
            }
            if (YD > 2) {
                YD = 0;
            }
        } else if (fx.equals(Fx.right)) {
            image = imageright[YD];
            moveidnum++;
            if (moveidnum > 5) {
                YD++;
                moveidnum = 0;
            }
            if (YD > 2) {
                YD = 0;
            }
        }
        if (Jump) {
            if (y <= 260) {
                Jump = false;
            } else {
                y -= SuDu;
            }
        }

        System.out.println(El);
        if (!Jump && !El) {
            y += SuDu;
            if (y >= 460) {
                Jump1 = true;
                if (Jump1) {
                    y = 460;
                    Jump1 = false;
                }

            }
        }
        if (x >= SuperMarioBros.Height / 2 + 100 && fx.equals(Fx.right)) {
            x = SuperMarioBros.Height / 2 + 100;
            Bj = true;
        } else {
            Bj = false;
        }
        move();
        super.pain(graphicsContext);
    }

    public void move() {
        jx = x;
        jy = y;
        switch (fx) {
            case left:
                x -= SuDu;
                break;
            case right:
                x += SuDu;
                break;
        }
        //判断一下人物是否出界
        if (x < 0) x = 0;
        if (x > SuperMarioBros.Width) x = SuperMarioBros.Width;
        impact();
    }

    public void redirect() {
        //判断人物方向
        if (leftYD && !rightYD) {
            fx = Fx.left;
        } else if (!leftYD && rightYD) {
            fx = Fx.right;
        } else if (!leftYD && !rightYD) {
            fx = Fx.stop;
        }
    }

    public void AnXia(KeyCode keyCode) {
        //按下按键
        switch (keyCode) {
            case A:
                leftYD = true;
                break;
            case D:
                rightYD = true;
                break;
        }

        if (keyCode == KeyCode.J && y == 460) {
            Jump = true;
        }
        redirect();

    }

    public void SongKai(KeyCode keyCode) {
        //松开按键
        switch (keyCode) {
            case A:
                leftYD = false;
                break;
            case D:
                rightYD = false;
                break;
        }
        //判断是否跳起
        if (keyCode == KeyCode.J) {
            Jump = false;
        }
        redirect();
    }

    public void impact() {
        for (int i = 0; i < GameScene.list.size(); i++) {

            //便利障碍物列表
            Sprite sprite = GameScene.list.get(i);

            if (sprite.getContour().intersects(getContour())) {
                //判断是否碰撞
//                if (Mushroom.class.isAssignableFrom(sprite.getClass())) {
//                    //判断是不是魔蘑菇
//                    if (sprite.x < this.x + 40 && sprite.y == this.y || sprite.x + 40 > this.x && sprite.y == this.y) {
//                        //判断是不是左边或者右边碰撞
//                        GameScene.GameOver();
//                    } else if (this.y + 40 < sprite.y) {
//                        //判断是不是踩蘑菇了
//                        pzjc = true;
//                    }
//                }

                if (Brick.class.isAssignableFrom(sprite.getClass()) || Gold.class.isAssignableFrom(sprite.getClass())) {

                    boolean flag = isBrick(this,(Brick) sprite);
                    if (flag){
                        break;
                    }
                }

//                if (Gold.class.isAssignableFrom(sprite.getClass())) {
//                    if (this.x + 60 >= sprite.x || this.x >= sprite.x) {
//                        Bj = false;
//                    }
//                    if (this.y + 60 >= sprite.y && this.x > sprite.x && this.x > sprite.x + 40) {
//
//                    }
//                }
            }else{
                El = false;
            }


        }

    }

    private boolean isBrick(MarioBros mario,Brick brick){
        // 1.判断是侧面接触还是上面接触
        // 砖头底外
        double upOut = brick.y,upInner = brick.y+10;
        double downOut = brick.y+40,downInner = brick.y+30;
        double leftOut = brick.x,leftInner = brick.x+10;
        double rightOut = brick.x+40,rightInner = brick.x+30;
        double marioUp = mario.y,marioDown = mario.y+60,marioLeft = mario.x,marioRight = mario.x+60;
        if (marioDown<=upInner){
//            System.out.println(((marioRight-leftOut)>10) || ((rightOut-marioLeft)>10));
            System.out.println("马里奥站在砖头上了");
            El = true;
            y = 301;
            return true;
        }
        if (marioUp<=downInner){
            System.out.println("马里奥顶到了砖头下里面");
            El = false;
            Jump = false;
            return true;
        }
        if (marioLeft>=rightInner){
            System.out.println("1111111111111111111111111111111");
            Jump = false;
            El=false;
            x = rightOut;
            return true;
        }
        if (marioRight<=leftInner){
            System.out.println("2222222222222222222222222222222");
            Jump = false;
            El = false;
            x = leftOut;
            return true;
        }
        return false;
    }


}
